package message

import (
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"
	"log"
	"my-gameserver-go/core/mynet/conn"
	"my-gameserver-go/logic/consts"
	"my-gameserver-go/logic/structs"
)

type Manager struct {
}

var InsMessageManager *Manager

func init() {
	InsMessageManager = &Manager{}
}

func (messageManager *Manager) ConnSendAndClose(conn *conn.GameConnection, message structs.ResMessage) {
	if conn.Isclosed {
		log.Println("conn has closed msgSendFail msgId:", message.MsgId)
		return
	}
	message.Callback = func() {
		//关闭连接的回调函数
		conn.Cancel()
	}
	conn.WriteMessage(message)
}

func (messageManager *Manager) ConnSend(conn *conn.GameConnection, message structs.ResMessage) {
	if conn.Isclosed {
		log.Println("conn has closed msgSendFail msgId:", message.MsgId)
		return
	}
	conn.WriteMessage(message)
}
func (messageManager *Manager) PlayerSend(conn *conn.GameConnection, protoMsgId protoreflect.Enum, protoMsg proto.Message) {
	if conn.Isclosed {
		log.Println("conn has closed msgSendFail msg:", protoMsg)
		return
	}
	resMsg, ok := structs.NewResMessageStruct(int32(protoMsgId.Number()), consts.SUCCESS_CODE, protoMsg)
	if !ok {
		return
	}
	conn.WriteMessage(resMsg)
}
